﻿using MyFileView.Core.Core;
using MyFileView.Core.DocumentFormat;

using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

using uno;

using unoidl.com.sun.star.beans;

namespace MyFleView.Core
{
    /// <summary>
    /// abstract要放到前面才嗯那个能确认为抽象类，在程序集中判断时
    /// </summary>
    abstract public  class AbstractOfficeDocumentConvert: IFilePreview
    {

        //public virtual IDictionary DefaultProperties
        //{
        //    get { return _defaultProperties; }
        //}
        public virtual string[] Name { get; }

        public virtual void FilePreview() { }


        public ConvertFileContext ConvertFileContext { get; set; }
   
        // public PropertyValue[]  DefaultPropertyValue { get { return ToPropertyValues(_defaultProperties); } }


        private readonly IDictionary _defaultProperties= new Hashtable();

   //     public Func<PropertyValue[]> FileProperty { get { return GetFileSuffixProperty; } }

      //  public IDocumentFormatRegistry DocumentFormatRegistry { get; set; }
     //   public ConvertFileInfo ConvertFileInfo { get; set ; }

        protected AbstractOfficeDocumentConvert(IConvertFileFactory convertFileFactory)//IDocumentFormatRegistry documentFormatRegistry
        {
            ConvertFileContext = convertFileFactory.ConvertFileContext;
            _defaultProperties["Hidden"] = true;
            _defaultProperties["ReadOnly"] = true;
            ConvertFileContext.DefaultProperties = ToPropertyValues(_defaultProperties);
        }
   

        ///// <summary>
        ///// 设置转化后文件的文件名
        ///// </summary>
        //public virtual void OutPutFileHandle(string newFileSuffix)
        //{
        //    ConvertFileContext.FileInfo.OutPutFullFilePath = newFileSuffix;
        //}

        /// <summary>
        /// 生成转化格式的参数
        /// </summary>
        /// <param name="storeProperties"></param>
        /// <returns></returns>
        public  PropertyValue[] GenerateFileConvertProperty(StorePropertiesDetail  storeProperties) 
        {
            IDictionary customProperties = new Hashtable();
            customProperties.Add(nameof(storeProperties.SPREADSHEET.FilterName), storeProperties.SPREADSHEET.FilterName);
            return ToPropertyValues(customProperties) ;
        }

        /// <summary>
        /// 转化为链接libreoffice的参数
        /// </summary>
        /// <param name="properties"></param>
        /// <returns></returns>
        public static PropertyValue[] ToPropertyValues( IDictionary properties)//internal
        {
            PropertyValue[] propertyValues = new PropertyValue[properties.Count];
            int i = 0;

            for (IEnumerator iter = new ListSupport(properties).GetEnumerator(); iter.MoveNext();)
            {
                if (iter.Current != null)
                {
                    DictionaryEntry entry = (DictionaryEntry)iter.Current;
                    Object valueRenamed = entry.Value;
                    var renamed = valueRenamed as IDictionary;
                    if (renamed != null)
                    {
                        // recursively Convert nested Map to PropertyValue[]
                        IDictionary subProperties = renamed;
                        valueRenamed = ToPropertyValues(subProperties);
                    }
                    propertyValues[i++] = Property((String)entry.Key, valueRenamed);
                }
            }
            return propertyValues;
        }
        protected internal static PropertyValue Property(String name, Object valueRenamed)
        {
            PropertyValue property = new PropertyValue
            {
                Name = name,
                Value = new Any(valueRenamed.GetType(), valueRenamed)
            };
            //property.Value = new uno.Any("writer_pdf_Export"); value_Renamed;
            return property;
        }

      
    }
}